/*sywsGGtTLb/wDzM0jlIiWu/BZJAgfVEqY6VdTYu+giAwdBRWGXVHz9RXyell2zq+*/
import { billType } from "../../../paybill/paybill/card/constants"
import { headButton, bodyButton, innerButton } from '../../../public/components/pubUtils/buttonName'
import { RecBillGenerateInvoiceTime, RecBillIsExistsIvApplication } from '../../../public/components/pubUtils/billPubUtil'


let billstatus = {//单据状态
    //9=未确认，-1=保存，1=审批通过，2=审批中，-99=暂存，8=签字，
    UNCOMFIRM: 9,
    SAVE: -1,
    AUDIT: 1,
    AUDITING: 2,
    TEMPEORARY: -99,
    SIGN: 8
}
let approvestatus = {//审批状态
    //-1=自由态，0=未通过态，1=通过态，2=进行中态，3=提交态， 
    NOSTATE: -1,
    NOPASS: 0,
    PASSING: 1,
    GOINGON: 2,
    COMMIT: 3
}
let effectstatus = {//生效状态
    //0=未生效，10=已生效
    EXECUTED: 10,
    INVALID: 0
}

let settleflagstatus = {
    WAITSETTLE: 0, //待结算
    SETTLESUCCED: 1,// 结算成功
    INSETTLE: 2,//结算中
    SOMEPAYFINISH: 11//
}

let coordflag = {//确认标志
    //0=非确认，1=确认
    VERIFY: 1,
    NOVERIFY: 0

}
let redbackType = {
    // 0 自制红冲，1止付红冲,2退票红冲，3取消核销红冲,4退款 红冲
    SELFMADE: "0",
    STOPPAYMENTRED: 1,
    FBMRED: 2,
    CANCELVERIFYRED: 3,
    REFUNDRED: 4
}
//来源项目管理单据类型
let projectBillType = ['4D65', '4D53', '4D83', '4D33', '4D39', '4D48', '4D50', '4D86']

//来源资产单据类型
let amBillType = ['4B36']
//来源采购订单付款计划(不允许增删行，复制行)
let addorDeleteLineBillTypr = ['21', '4A3A', '4A77']
//根据状态和数据确认按钮的可见性
//status 当前页面状态，列表态传入空值
//data 当前的主表数据
//id 按钮id

let trueBtn = ['Add_blank', 'Add', 'Head_g1', 'adcode', 'paca', 'F0', 'F1', 'F2', 'F3', 'FCT1',
    'FCT2', '36D1', '4A18', '4A23', '4A49', '4A24', '4A2F', '4A3F', '4A3H', '4BL9', '4A77', '4BL3', '4A3D', '4A87', '4A3L', '4BLA']


let listTrueBtn = ['Add_blank', 'Add', 'Head_g1', 'adcode', 'paca', 'F0', 'F1', 'F2', 'F3', 'FCT1',
    'FCT2', '36D1', '4A18', '4A23', '4A49', '4A24', '4A2F', '4A3F', '4A3H', 'More', 'InitBuild', 'CancelInitBuild', 'ImportData', 'Refresh', 'ConnectSettleInfo', '4BL9', '4A77', '4BL3', '4A3D', '4A87', '4A3L', '4BLA']

/**
 * 
 * @param {*} status 浏览态还是编辑态，默认浏览态
 * @param {*} data 数据
 * @param {*} id 按钮id
 * @param {*} type  list 表示列表控制按钮
 */
let buttonVisible = function (status, data, id, type, that) {
    if (!data || !(data.pk_tradetype ? data.pk_tradetype.value : null)) {
        if (type == 'list') {
            if (listTrueBtn.indexOf(id) != -1) {
                return true
            } else {
                return false

            }
        } else {
            if (that.props.getUrlParam('status')) {
                if (trueBtn.indexOf(id) != -1) {
                    return true
                } else {
                    return false
                }
            } else {
                return false
            }

        }
    }
    if (!status) {
        status = 'browse'//默认status为browse
    }
    let flag = true//按钮的状态，默认为true
    let money = data.money ? data.money.value : null
    let pk_billtype = data.pk_billtype ? data.pk_billtype.value : null
    let coordflagInfo = data.coordflag ? data.coordflag.value : null//确认标志信息
    let approvestatusInfo = data.approvestatus ? data.approvestatus.value : null //审批状态信息
    let billstatusInfo = data.billstatus ? data.billstatus.value : null//单据状态信息
    let effectstatusInfo = data.effectstatus ? data.effectstatus.value : null  //生效状态信息
    let settleflag = data.settleflag ? data.settleflag.value : null  //结算状态信息
    let isinitInfo = data.isinit ? data.isinit.value : null//期初信息 
    let redTypeStatus = data.redbacktype ? data.redbacktype.value : null//红冲类型
    let pk_org_v = data.pk_org_v ? data.pk_org_v.value : null//组织信息
    let isforce = data.isforce ? data.isforce.value : false//是否承付
    let isrefund = data.isrefund ? data.isrefund.value : false
    let scene//场景信息
    // 发票按钮转换
    var newid = id
    if (newid != 'LinkTaxInvoice') {
        newid = (newid.indexOf('Invoice') == -1 && newid.indexOf('invoice') == -1)
            || (newid == 'RedInvoice' || newid == 'MaintainTaxInvoice' || newid == 'InvoiceUploader' || newid == 'InvoiceMenu')
            ? id : 'invoice'
    }
    switch (newid) {
        /**
         * 头部按钮
         */
        case 'Refund'://退款
            if (status != 'browse' || effectstatusInfo != effectstatus.EXECUTED || isrefund) {
                flag = false
            }
            break
        case 'CancelRefund'://取消退款
            if (status != 'browse' || (pk_billtype == "F0" && !isrefund) || (pk_billtype == "F2" && isrefund && money < 0) || (pk_billtype == "F2" && !isrefund)) { //isrefund 是false 或者未定义 或者null
                flag = false
            }
            break
        case 'Generate'://生成
            if (status != 'browse' || effectstatusInfo != effectstatus.EXECUTED) {
                flag = false
            }
            break
        case 'SendSP'://传结算
            if (status != 'browse' || (settleflag != settleflagstatus.WAITSETTLE || approvestatusInfo != approvestatus.PASSING) || redbackType.STOPPAYMENTRED == redTypeStatus) {
                flag = false
            }
            break
        case 'CancelSendSP'://取消结算
            if (status != 'browse' || settleflag == settleflagstatus.WAITSETTLE || approvestatusInfo != approvestatus.PASSING) {
                flag = false
            }
            break
        case 'More':
            if (status != 'browse') {
                flag = false
            }
            break
        case 'CancelConfirm'://取消确认
            if (status != 'browse' || coordflagInfo == coordflag.NOVERIFY) {
                flag = false
            }
            break
        case 'Confirm'://确认
            if (coordflagInfo == coordflag.VERIFY) {
                flag = false
            }
            break
        case 'Add'://新增
            if (status != 'browse') {
                flag = false
            }
            break
        case 'Edit'://修改
            //单据状态为暂存和保存态可以修改
            if (status != 'browse' || !(isinitInfo == true || billstatusInfo == billstatus.TEMPEORARY || billstatusInfo == billstatus.SAVE)) {
                flag = false
            }
            break
        case 'Save'://保存
            if (status == 'browse') {
                flag = false
            }
            break
        case 'TempSave'://暂存
            //暂存态单据可暂存
            if (status == 'browse' || (status == 'edit' && billstatusInfo != billstatus.TEMPEORARY)) {
                flag = false
            }
            break
        case 'SaveAndCommit'://保存提交
            //浏览态或审批中不能有保存提交按钮
            if (status == 'browse' || approvestatusInfo != approvestatus.NOSTATE) {
                flag = false
            }
            //承付单据,不等于承付中不可用
            if (type == 'card' && isforce && that.props.cardTable.getValByKeyAndIndex(that.tableId, 0, 'commpaystatus')
                && that.props.cardTable.getValByKeyAndIndex(that.tableId, 0, 'commpaystatus').value != '1') {
                flag = false
            }
            break
        case 'Delete'://删除
            //浏览态 审批状态为自由态 
            if (status != 'browse' || !(isinitInfo == true || approvestatusInfo == approvestatus.NOSTATE)) {
                flag = false
            }
            break
        case 'Refresh'://刷新
            if (status != 'browse') {
                flag = false
            } else if (status == 'browse' && that.props.getUrlParam('type') == 'transfer') {
                flag = true
            }
            break
        case 'Cancel'://取消
            if (status == 'browse') {
                flag = false
            }
            break
        case 'Copy'://复制
            if (status != 'browse') {
                flag = false
            }
            break
        case 'Commit'://提交
            //自由态单据且单据状态为保存态单据
            if (status != 'browse' || !(approvestatusInfo == approvestatus.NOSTATE && billstatusInfo == billstatus.SAVE)) {
                flag = false
            }
            //承付单据提交按钮判断
            // if(type == 'card' && isforce && that.props.cardTable.getValByKeyAndIndex(that.tableId, 0, 'commpaystatus')
            //     && that.props.cardTable.getValByKeyAndIndex(that.tableId, 0, 'commpaystatus').value == '0'){
            //     flag = false
            // }
            break
        case 'Uncommit'://收回
            if (that) {
                scene = that.props.getUrlParam('scene')
            }
            //所有场景提交或者已经生效有收回，默认的审批通过也有收回,报账场景走默认 //已经退款不能收回
            if (scene && scene != 'bz') {
                if (status != 'browse' || !(approvestatusInfo == approvestatus.COMMIT || effectstatusInfo == effectstatus.EXECUTED) || isrefund) {
                    flag = false
                }
            } else {
                if (status != 'browse' || !(approvestatusInfo == approvestatus.COMMIT ||
                    approvestatusInfo == approvestatus.PASSING || effectstatusInfo == effectstatus.EXECUTED) || (isrefund && pk_billtype != 'F3')) {
                    flag = false
                }
            }
            break
        case 'SendMail'://发送邮件
            if (status != 'browse' || !(approvestatusInfo == approvestatus.PASSING)) {
                flag = false
            }
            break
        case 'Approve'://审批
            if (status != 'browse' || effectstatusInfo == effectstatus.EXECUTED) {
                flag = false
            }
            break
        case 'UnApprove'://取消审批取消审批
            if (status != 'browse' || effectstatusInfo == effectstatus.INVALID) {
                flag = false
            }
            break
        case 'MadeBill'://制单
            //生效状态为已生效
            let ispaygather = ('F3' == pk_billtype || 'F2' == pk_billtype) && approvestatusInfo == approvestatus.PASSING
            if ((status != 'browse' || !(effectstatusInfo == effectstatus.EXECUTED)) && !ispaygather) {
                flag = false
            }
            break
        case 'BodyVerify'://按表体核销
            //单据状态为暂存无法核销
            if (status != 'browse' || billstatusInfo == billstatus.TEMPEORARY || isrefund) {
                flag = false
            }

            break
        case 'WholeVerify'://按整单核销
            //单据状态为暂存无法核销
            if (status != 'browse' || billstatusInfo == billstatus.TEMPEORARY || isrefund) {
                flag = false
            }
            break
        case 'RedBack'://红冲
            //生效状态为已生效
            if (status != 'browse' || !(effectstatusInfo == effectstatus.EXECUTED) || isrefund) {
                flag = false
            }
            break
        case 'AttachManage'://附件管理
            if (status != 'browse') {
                flag = false
            }
            break
        case 'CommisionPay'://承付
            if (status != 'browse' || !isforce) {
                flag = false
            }
            break
        case 'CancelCommisionPay'://取消承付
            if (status != 'browse' || !isforce) {
                flag = false
            }
            break
        case 'Receipt'://影像管理
            if (status != 'browse') {
                flag = false
            }
            break
        case 'ReceiptCheck'://影像查看
            if (status != 'browse') {
                flag = false
            }
            break
        case 'ReceiptScan'://影像扫描  
            if (status != 'browse') {
                flag = false
            }
            break
        case 'InvoiceUploader'://上传电子发票 
            if (status != 'browse' || approvestatusInfo == approvestatus.PASSING) {
                flag = false
            }
            break
        case 'BillLinkQuery'://联查单据
            if (status != 'browse') {
                flag = false
            }
            break
        case 'LinkBal'://联查余额表
            if (status != 'browse') {
                flag = false
            }
            break
        case 'LinkDeal'://联查处理情况
            if (status != 'browse') {
                flag = false
            }
            break
        case 'LinkVouchar'://联查凭证
            if (status != 'browse') {
                flag = false
            }
            break
        case 'LinkConfer'://联查协同该单据
            if (status != 'browse') {
                flag = false
            }
            break
        case 'LinkTerm'://联查收付款协议
            if (status != 'browse') {
                flag = false
            }
            break
        case 'LinkTbb'://联查计划预算
            if (status != 'browse') {
                flag = false
            }
            break
        case 'LinkInformer'://到账通知
            if (status != 'browse') {
                flag = false
            }
            break
        case 'LinkSettleInfo'://结算信息
            if (status != 'browse') {
                flag = false
            }
            break
        case 'LinkAprv'://审批详情 
            if (status != 'browse') {
                flag = false
            }
            break
        case 'ImportData'://导入
            if (status != 'browse') {
                flag = false
            }
            break
        case 'ExportData'://导出
            if (status != 'browse') {
                flag = false
            }
            break
        case 'Print'://打印
            if (status != 'browse') {
                flag = false
            }
            break
        case 'Output'://输出
            if (status != 'browse') {
                flag = false
            }
            break
        case 'OfficalPrint'://正式打印
            if (status != 'browse') {
                flag = false
            }
            break
        case 'CancelPrint'://取消正式打印
            if (status != 'browse') {
                flag = false
            }
            break
        // case 'SpecialInvoice'://专用发票
        //     //浏览态 审批状态为审批通过
        //     if (status != 'browse' || approvestatusInfo != approvestatus.PASSING || money < 0) {
        //         flag = false;
        //     }
        //     break;
        // case 'GeneralInvoice'://普通发票
        //     //浏览态 审批状态为审批通过
        //     if (status != 'browse' || approvestatusInfo != approvestatus.PASSING || money < 0) {
        //         flag = false;
        //     }
        //     break;
        // case 'Electronspecinvoice'://电子专票
        //     //浏览态 审批状态为审批通过
        //     if (status != 'browse' || approvestatusInfo != approvestatus.PASSING || money < 0) {
        //         flag = false;
        //     }
        //     break;
        // case 'ElectronicInvoice'://电子发票
        //     //浏览态 审批状态为审批通过
        //     if (status != 'browse' || approvestatusInfo != approvestatus.PASSING || money < 0) {
        //         flag = false;
        //     }
        //     break;
        // case 'OilSpecialInvoice'://专用发票(成品油)
        //     //浏览态 审批状态为审批通过
        //     if (status != 'browse' || approvestatusInfo != approvestatus.PASSING || money < 0) {
        //         flag = false;
        //     }
        //     break;
        // case 'OilGeneralInvoice'://普通发票(成品油)
        //     //浏览态 审批状态为审批通过
        //     if (status != 'browse' || approvestatusInfo != approvestatus.PASSING || money < 0) {
        //         flag = false;
        //     }
        //     break;
        // case 'OilElectronicInvoice'://电子发票(成品油)
        //     //浏览态 审批状态为审批通过
        //     if (status != 'browse' || approvestatusInfo != approvestatus.PASSING || money < 0) {
        //         flag = false;
        //     }
        //     break;
        case 'invoice': // 发票按钮通用标识
            //浏览态 审批状态为审批通过
            if (status != 'browse' || approvestatusInfo != approvestatus.PASSING || money < 0) {
                flag = false
            }
            if ('LinkInvoice' == id && status == 'browse' && money > 0) {
                flag = true//联查发票展示
            }
            break
        case 'RedInvoice'://红字发票
            //浏览态 审批状态为审批通过
            if (status != 'browse' || approvestatusInfo != approvestatus.PASSING || money >= 0) {
                flag = false
            }
            break
        case 'MaintainTaxInvoice'://维护税务发票 
            //浏览态 审批状态为审批通过
            if (status != 'browse' || approvestatusInfo != approvestatus.PASSING) {
                flag = false
            }
            break
        case 'LinkTaxInvoice'://联查税务发票
            //浏览态 审批状态为审批通过
            if (status != 'browse') {
                flag = false
            }
            break

        /**
         * 以下是卡片界面的肩部按钮
         */

        case 'Pausetrans'://挂起
            //生效状态为已生效的才可以过去和取消挂起
            if (status != 'browse' || !(effectstatusInfo == effectstatus.EXECUTED) || isrefund) {
                flag = false
            }
            break
        case 'Cancelpause'://取消挂起
            if (status != 'browse' || !(effectstatusInfo == effectstatus.EXECUTED) || isrefund) {
                flag = false
            }
            break
        case 'PrePay'://预收付
            //生效状态为已生效的才可以预收付
            if (status != 'browse' || !(effectstatusInfo == effectstatus.EXECUTED) || isrefund) {
                flag = false
            }
            break
        case 'AddLine'://增行
            if (status == 'browse') {
                flag = false
            }
            //拉单生成的单据不允许插入行
            if (type == 'card' && (status == 'edit' || status == 'add')) {
                let bodyrows = that.props.createMasterChildData(that.getPagecode(), that.formId, that.tableId).body[that.tableId].rows
                for (let i = 0; i < bodyrows.length; i++) {
                    let top_billid = bodyrows[i].values.top_billid.value
                    let top_itemid = bodyrows[i].values.top_itemid.value
                    let top_billtype = bodyrows[i].values.top_billtype.value
                    if ((top_billid || top_itemid) && (top_billtype && top_billtype != '36S3')) {
                        flag = false
                        break
                    }
                }
            }
            break
        case 'DelLine'://删除行
            if (status == 'browse' || redTypeStatus == redbackType.FBMRED) {
                flag = false
            }
            break
        case 'CopyLine'://复制行
            if (status == 'browse' || redTypeStatus == redbackType.FBMRED) {
                flag = false
            }
            break
        case 'PasteToEndLine'://粘行到末尾
            // if (status == 'browse' ) {
            flag = false
            //}
            break
        case 'CancelLine'://肩部取消
            // if (status == 'browse') {
            flag = false
            //}
            break
        /**
         * 表体行按钮
         */
        case 'Approve_inner'://审批
            if (status != 'browse' || effectstatusInfo == effectstatus.EXECUTED) {
                flag = false
            }
            break
        case 'UnApprove_inner'://取消审批取消审批
            if (status != 'browse' || effectstatusInfo == effectstatus.INVALID) {
                flag = false
            }
            break
        case 'MadeBill_inner'://制单
            //生效状态为已生效
            let isF3orF2 = ('F3' == pk_billtype || 'F2' == pk_billtype) && approvestatusInfo == approvestatus.PASSING
            if ((status != 'browse' || !(effectstatusInfo == effectstatus.EXECUTED)) && !isF3orF2) {
                flag = false
            }
            break
        case 'Edit_inner': //修改行按钮
            //单据状态为暂存和保存态可以修改
            if (status != 'browse' || !(isinitInfo == true || billstatusInfo == billstatus.TEMPEORARY || billstatusInfo == billstatus.SAVE)) {
                flag = false
            }
            break
        case 'Delete_inner': //删除行按钮
            //浏览态 审批状态为自由态
            if (status != 'browse' || !(isinitInfo == true || approvestatusInfo == approvestatus.NOSTATE) || redTypeStatus == redbackType.FBMRED) {
                flag = false
            }
            break
        case 'Delete_Inner': //删除行按钮
            //浏览态 审批状态为自由态
            if (status != 'browse' || !(isinitInfo == true || approvestatusInfo == approvestatus.NOSTATE) || redTypeStatus == redbackType.FBMRED) {
                flag = false
            }
            break
        case 'Copy_inner'://复制行按钮
            if (status != 'browse' || redTypeStatus == redbackType.FBMRED) {
                flag = false
            }
            break
        case 'CancelConfirm_inner'://取消确认行按钮
            if (coordflagInfo == coordflag.NOVERIFY) {
                flag = false
            }
            break
        case 'Confirm_inner'://确认行按钮
            if (coordflagInfo == coordflag.VERIFY) {
                flag = false
            }
            break
        case 'Commit_inner'://提交
            //自由态单据且单据状态为保存态单据
            if (status != 'browse' || !(approvestatusInfo == approvestatus.NOSTATE && billstatusInfo == billstatus.SAVE)) {
                flag = false
            }
            break
        case 'Uncommit_inner'://收回
            if (that) {
                scene = that.props.getUrlParam('scene')
            }
            //所有场景提交或者已经生效有收回，默认的审批通过也有收回
            if (scene && scene != 'bz') {
                if (status != 'browse' || !(approvestatusInfo == approvestatus.COMMIT || effectstatusInfo == effectstatus.EXECUTED) || isrefund) {
                    flag = false
                }
            } else {
                if (status != 'browse' || !(approvestatusInfo == approvestatus.COMMIT ||
                    approvestatusInfo == approvestatus.PASSING || effectstatusInfo == effectstatus.EXECUTED) || isrefund) {
                    flag = false
                }
            }
            break
        case 'open_browse'://展开
            if (status != 'browse' && that.isLineOpen) {
                flag = false
            }
            break
        case 'close_browse'://收回
            if (status != 'browse' && !that.isLineOpen) {
                flag = false
            }
            break
        case 'open_edit'://弹窗编辑
            if (status == 'browse') {
                flag = false
            }
            break

        default:
            if ((type == 'list' && listTrueBtn.indexOf(id) == -1)) {
                flag = false
            }
            break


    }
    return flag
}

//获取到所有的btn
//inBtn 传入的按钮
//outBtenKey 所有的不属于行按钮的key
let getButtonsKey = function (inBtn, outBtnKey) {
    for (let i = 0; i < inBtn.length; i++) {
        if (inBtn[i].area != 'list_inner' && inBtn[i].area != 'card_inner') {
            if (inBtn[i].children.length == 0) {
                outBtnKey.push(inBtn[i].key)
            } else {
                outBtnKey.push(inBtn[i].key)
                getButtonsKey(inBtn[i].children, outBtnKey)
            }
        }
    }
    return outBtnKey
}

//获取所有的inner
let getInnerButtonkey = function (buttons) {
    let innerButtons = []
    for (let i = 0; i < buttons.length; i++) {
        if (buttons[i].area == 'list_inner' || buttons[i].area == 'card_inner') {
            innerButtons.push(buttons[i].key)
        }
    }
    return innerButtons

}

//获取卡片界面的所有的肩部按钮和行按钮用于控制卡片行和肩部按钮的可见性
let cardGetbodyAndInnerbutton = function (buttons) {
    let bodyAndInnerbutton = []
    for (let i = 0; i < buttons.length; i++) {
        if (buttons[i].area == 'card_body' || buttons[i].area == 'card_inner') {
            bodyAndInnerbutton.push(buttons[i].key)
        }
    }
    return bodyAndInnerbutton

}

/**单据列表对按钮的控制 */
let onListButtonControl = function (that) {
    let selectedData = that.props.table.getCheckedRows(that.tableId)
    if (selectedData.length == 1) {
        let billtype = selectedData[0].data.values ? (selectedData[0].data.values.pk_billtype ? selectedData[0].data.values.pk_billtype.value : null) : null
        let bilstatus = selectedData[0].data.values ? (selectedData[0].data.values.billstatus ? selectedData[0].data.values.billstatus.value : null) : null//selectedData[0].data.values.billstatus.value;
        let tradetype = selectedData[0].data.values ? (selectedData[0].data.values.pk_tradetype ? selectedData[0].data.values.pk_tradetype.value : null) : null//selectedData[0].data.values.pk_tradetype.value;
        if (billtype == 'F0' && (bilstatus == billstatus.SAVE || bilstatus == billstatus.AUDITING)) {
            dealButtonsForF0(selectedData[0].data.values, that, tradetype)
        } else {
            dealButtons(selectedData[0].data.values, that)
        }
    } else if (selectedData.length > 1) {
        that.props.button.setButtonDisabled(that.Info.allButtonsKey, false)
    } else {
        dealButtons(null, that)
    }
}

let cardPullButtonControl = function (that) {
    if (that.Info.pullBillInfoVOAry) {
        let trueBtn = [] //可见的按钮
        let falseBtn = [] //不可见的按钮
        for (let i = 0; i < that.Info.pullBillInfoVOAry.length; i++) {
            let flag = buttonVisible(null, null, that.Info.pullBillInfoVOAry[i].src_billtype, 'card', that)
            if (flag) {
                trueBtn.push(that.Info.pullBillInfoVOAry[i].src_billtype)
            } else {
                falseBtn.push(that.Info.pullBillInfoVOAry[i].src_billtype)
            }
        }
        that.props.button.setButtonDisabled(trueBtn, false)
        that.props.button.setButtonDisabled(falseBtn, true)
    }
}

let dealButtons = (record, that) => {
    let trueBtn = [] //可见的按钮
    let falseBtn = [] //不可见的按钮
    for (let i = 0; i < that.Info.allButtonsKey.length; i++) {
        let flag = buttonVisible(null, record, that.Info.allButtonsKey[i], 'list', that)
        if (flag) {
            trueBtn.push(that.Info.allButtonsKey[i])
        } else {
            falseBtn.push(that.Info.allButtonsKey[i])
        }
    }
    that.props.button.setButtonDisabled(trueBtn, false)
    that.props.button.setButtonDisabled(falseBtn, true)
}

/**
 * 专供应收单处理发票使用
 * @param {} record 
 * @param {*} that 
 */
let dealButtonsForF0 = (record, that, tradetype) => {
    let trueBtn = [] //可见的按钮
    let falseBtn = [] //不可见的按钮
    let timeflag = RecBillGenerateInvoiceTime(tradetype)
    for (let i = 0; i < that.Info.allButtonsKey.length; i++) {
        let flag = buttonVisible(null, record, that.Info.allButtonsKey[i], 'list', that)
        if (flag) {
            trueBtn.push(that.Info.allButtonsKey[i])
        } else {
            let isAdd = that.Info.allButtonsKey[i] == headButton.SpecialInvoice || that.Info.allButtonsKey[i] == headButton.GeneralInvoice
                || that.Info.allButtonsKey[i] == headButton.Electronspecinvoice || that.Info.allButtonsKey[i] == headButton.ElectronicInvoice
                || that.Info.allButtonsKey[i] == headButton.OilSpecialInvoice || that.Info.allButtonsKey[i] == headButton.OilGeneralInvoice
                || that.Info.allButtonsKey[i] == headButton.OilElectronicInvoice || that.Info.allButtonsKey[i] == headButton.RedInvoice
                || that.Info.allButtonsKey[i] == headButton.MaintainTaxInvoice || that.Info.allButtonsKey[i] == headButton.LinkTaxInvoice
                || that.Info.allButtonsKey[i] == headButton.VatinvoiceVehicleInvoice_99
            if (!timeflag) {
                if (!isAdd) {
                    falseBtn.push(that.Info.allButtonsKey[i])
                }
            } else {
                falseBtn.push(that.Info.allButtonsKey[i])
            }
        }
    }
    if (!timeflag) {
        let money = record.money ? record.money.value : 0
        if (money > 0) {
            trueBtn.push(headButton.SpecialInvoice)
            trueBtn.push(headButton.GeneralInvoice)
            trueBtn.push(headButton.Electronspecinvoice)
            trueBtn.push(headButton.ElectronicInvoice)
            trueBtn.push(headButton.OilSpecialInvoice)
            trueBtn.push(headButton.OilGeneralInvoice)
            trueBtn.push(headButton.OilElectronicInvoice)
            trueBtn.push(headButton.VatinvoiceVehicleInvoice_99)
        } else {
            trueBtn.push(headButton.RedInvoice)
            trueBtn.push(headButton.MaintainTaxInvoice)
            trueBtn.push(headButton.LinkTaxInvoice)
        }
    }
    that.props.button.setButtonDisabled(trueBtn, false)
    that.props.button.setButtonDisabled(falseBtn, true)
}


/**
 * 卡片点击行
 * 浏览态控制按钮状态,主要控制挂起，取消挂起和预收付按钮
 * 编辑态控制按钮状态
 */
let onSelectedCardBodyEditControl = function (that) {
    let status = that.props.getUrlParam('status')
    if (!status) {
        status = 'browse'
    }
    if (status == 'browse') {//浏览态
        let selectedData = that.props.cardTable.getCheckedRows(that.tableId)
        if (selectedData.length == 1) {
            let pausetransact = selectedData[0].data.values.pausetransact ? selectedData[0].data.values.pausetransact.value : null//挂起标志 1是  0 否
            let prepay = selectedData[0].data.values.prepay ? selectedData[0].data.values.prepay.value : null//预收付标志0=应付款，1=预付款
            let trueBtn = []
            let falseBtn = []
            if (pausetransact == '0' || !pausetransact) {
                trueBtn.push('Pausetrans')
                falseBtn.push('Cancelpause')
            } else if (pausetransact) {
                trueBtn.push('Cancelpause')
                falseBtn.push('Pausetrans')
            }
            if (prepay == '0' || !prepay) {
                trueBtn.push('PrePay')
            } else {
                falseBtn.push('PrePay')
            }
            that.props.button.setButtonDisabled(trueBtn, false)
            that.props.button.setButtonDisabled(falseBtn, true)
        } else if (selectedData.length > 1) {
            that.props.button.setButtonDisabled(['Pausetrans', 'Cancelpause', 'PrePay'], false)
        } else {
            that.props.button.setButtonDisabled(['Pausetrans', 'Cancelpause', 'PrePay'], true)
        }
        if (selectedData) {
            //资产4BL9 4A77
            for (let i = 0; i < selectedData.length; i++) {
                if (selectedData && selectedData[i].data && selectedData[i].data.values) {
                    if (selectedData[i].data.values.pk_billtype.value == 'F0' && selectedData[i].data.values.top_billtype && (selectedData[i].data.values.top_billtype.value == '4BL9' || selectedData[i].data.values.top_billtype.value == '4A77')) {
                        that.props.button.setButtonDisabled(['DelLine'], true)
                    }
                }
            }
        }
    } else {//编辑态
        let head = that.props.createMasterChildData(that.props.getUrlParam('pagecode'), that.formId, that.tableId)
            .head[that.formId]
        let cardhead = head.rows[0].values
        let pk_org_v = cardhead.pk_org_v ? cardhead.pk_org_v.value : null
        if (pk_org_v) {
            let selectedData = that.props.cardTable.getCheckedRows(that.tableId)
            if (selectedData.length > 0) {
                that.props.button.setButtonDisabled(['CopyLine', 'DelLine'], false)
            } else {
                that.props.button.setButtonDisabled(['CopyLine', 'DelLine'], true)
            }
        } else {
            //pk_org不存在的时候，不做行按钮的处理
        }
    }
}

/**初始化卡片行的控制，主要是挂起等按钮的控制 */
let initCardBodybrowseControl = function (that) {
    that.props.button.setButtonDisabled(['Pausetrans', 'Cancelpause', 'PrePay'], true)
}

/**初始化卡片行的控制，主要是增行等按钮的控制 */
let initCardBodyEditControl = function (props, pk_org_v, that) {
    //存在上游的单据不允许新增行、插入行
    // if (that) {
    //     let bodyrows = that.props.createMasterChildData(that.getPagecode(), that.formId, that.tableId).body[that.tableId].rows;
    //     for (let i = 0; i < bodyrows.length; i++) {
    //         let top_billid = bodyrows[i].values.top_billid.value
    //         let top_itemid = bodyrows[i].values.top_itemid.value
    //         if (top_billid || top_itemid) {
    //             props.button.setButtonVisible(['AddLine'], false);
    //             break
    //         }
    //     }
    // }
    if (!pk_org_v) {
        props.button.setButtonDisabled(['AddLine', 'CopyLine', 'DelLine'], true)
    } else {
        props.button.setButtonDisabled(['AddLine'], false)
        props.button.setButtonDisabled(['CopyLine', 'DelLine'], true)
    }
}

/**
 * 卡片表体行按钮的控制
 * @param {*} props 
 * @param {*} buttonfalg 
 */
let cardBodyControl = function (props, buttonfalg, that) {
    props.button.setButtonVisible(['PasteToEndLine', 'CancelLine'], !buttonfalg)
    if (props.getUrlParam('type') === 'transfer') {
        props.button.setButtonVisible(['CopyLine', 'DelLine'], buttonfalg)
    } else {
        //拉单生成的单据不允许插入行
        if (that) {
            let bodyrows = that.props.createMasterChildData(that.getPagecode(), that.formId, that.tableId).body[that.tableId].rows
            for (let i = 0; i < bodyrows.length; i++) {
                let top_billid = bodyrows[i].values.top_billid ? bodyrows[i].values.top_billid.value : null
                let top_itemid = bodyrows[i].values.top_itemid ? bodyrows[i].values.top_itemid.value : null
                let top_billtype = bodyrows[i].values.top_billtype ? bodyrows[i].values.top_billtype.value : null
                if (top_billid || top_itemid) {
                    props.button.setButtonVisible(['CopyLine', 'DelLine'], buttonfalg)
                    if (top_billtype && top_billtype == '36S3') {
                        props.button.setButtonVisible(['CopyLine', 'DelLine', 'AddLine'], buttonfalg)
                    }
                    return
                }
            }
        }
        props.button.setButtonVisible(['CopyLine', 'DelLine', 'AddLine'], buttonfalg)
    }
}

/**
 * 
 * @param {*} status 当前表格状态
 * @param {*} buttonfalg 当前点击按钮之后改变的状态，用于判断显示那些按钮
 */
let cardBodyAndInnerButtonVisible = function (that, buttonfalg, expandRowStatus) {
    let tableStatus = that.props.cardTable.getStatus(that.tableId)
    let src = that.props.getUrlParam("src")
    let cmp_billtype = ['36J5', '36J7', '36J3']
    if (expandRowStatus && tableStatus == 'browse') {
        return ['Close_browse']//收起
    } else if (tableStatus == 'browse') {
        return ['open_browse']//展开
    } else if (tableStatus == 'edit') {
        if (buttonfalg == true) {//true
            if (that.props.getUrlParam('type') === 'transfer') {
                return ['open_edit', 'Copy_inner', 'Delete_inner']
            } else {
                //拉单生成的单据不允许插入行
                let bodyrows = that.props.createMasterChildData(that.getPagecode(), that.formId, that.tableId).body[that.tableId].rows
                for (let i = 0; i < bodyrows.length; i++) {
                    let top_billid = bodyrows[i].values.top_billid ? bodyrows[i].values.top_billid.value : null
                    let top_itemid = bodyrows[i].values.top_itemid ? bodyrows[i].values.top_itemid.value : null
                    let top_billtype = bodyrows[i].values.top_billtype ? bodyrows[i].values.top_billtype.value : null
                    if (top_billid || top_itemid) {
                        if (top_billtype && (top_billtype == '4BL9' || top_billtype == '4A77')) {//资产经营性结算以及周转材租出结算不允许删行
                            return ['open_edit', 'Copy_inner']
                        } else if (top_billtype && cmp_billtype.indexOf(top_billtype) != -1) { //委托收，委托付，不允许复制行，删行
                            return ['open_edit']
                        }
                        return ['open_edit', 'Copy_inner', 'Delete_inner']
                    }
                }
                if (src && src == "36S3") {//到账通知推单
                    return ['Delete_inner']
                }
                return ['open_edit', 'Copy_inner', 'Insert_inner', 'Delete_inner']
            }
        } else if (buttonfalg == false) {//false
            return ['Paste_inner']
        } else {
            if (src && src == "36S3") {//到账通知推单
                return ['Delete_inner']
            }
            return []
        }
    } else {
        return []
    }

}



//转单场景下，部分按钮不可见
export function dealTranferBtns (falseBtn) {
    if (this.props.getUrlParam('type') === 'transfer') {
        this.setState({ tpflag: false })//设置交易类型不可见
        falseBtn.push('Add')
        falseBtn.push('TempSave')
        falseBtn.push('Copy')
        falseBtn.push('Verify')
        falseBtn.push('RedBack')
        // falseBtn.push('ConnectSettleInfo')
        // falseBtn.push('LinkInformer')
        // falseBtn.push('LinkConfer')
        // falseBtn.push('LinkAprv')
        // falseBtn.push('ImportData')
        falseBtn.push('Pause_g1')
        falseBtn.push('PrePay')
        falseBtn.push('AddLine')
        falseBtn.push('Insert_inner')
        //falseBtn.push('Refresh')
    }
    if (this.props.getUrlParam('src') || this.props.getUrlParam('srcbilltype')) {
        falseBtn.push('TempSave')//外系统单据不支持暂存
        let srcbilltype = this.props.getUrlParam('srcbilltype')
        let srcbill = ['FCT2', 'FCT1', 'Z3']//收付款合同要复制行粘贴行
        if (srcbill.indexOf(srcbilltype) == -1) {
            falseBtn.push('CopyLine')
            falseBtn.push('DelLine')
        }

    }

}
//来源项目管理单据，部分按钮不可见
export function dealSrcProjectBtns (falseBtn, firstRow) {
    if (firstRow.length > 0 && (firstRow[0])) {
        let top_billtype = ((firstRow[0].values) && (firstRow[0].values.top_billtype)) ? firstRow[0].values.top_billtype.value : null
        if (projectBillType.indexOf(top_billtype) != -1 || addorDeleteLineBillTypr.indexOf(top_billtype) != -1 || amBillType.indexOf(top_billtype) != -1) {

            falseBtn.push('AddLine')
            falseBtn.push('CopyLine')
            falseBtn.push('DelLine')
            falseBtn.push('Insert_inner')
            falseBtn.push('Copy_inner')
            falseBtn.push('Delete_inner')
        }
    }

}

//侧拉增删行控制
export function addAndDelBtninModule (oper) {
    //top_billtype 最好可以获取当前行的，这样可以不用循环
    let top_billtype = this.props.cardTable.getColValue(this.tableId, 'top_billtype')
    let addtureType = ['21', '4A3A', '36J5', '36J7', '36J3', '36D1', '36D7', '4B36']
    let deltureType = ['21', '4A3A', '36J5', '36J7', '36J3', '4B36']
    let billtype = []
    let status = this.props.getUrlParam('status')

    for (let i = 0; i < top_billtype.length; i++) {
        if (top_billtype && top_billtype[i] && top_billtype[i].value) {
            if (oper && oper == 'Add') {
                if (addtureType.indexOf(top_billtype[i].value) != -1) {
                    billtype.push(top_billtype[i].value)
                }
            } else {
                if (deltureType.indexOf(top_billtype[i].value) != -1) {
                    billtype.push(top_billtype[i].value)
                }
            }

        }
    }
    //增删行控制， 退票红冲  取消核销红冲  禁用侧拉增删行  
    let redType = this.props.form.getFormItemsValue(this.formId, 'redbacktype')
    if (redType && redType.value && (redType.value == 2 || redType.value == 3)) {
        return true
    }
    if (billtype && billtype.length > 0) {
        return true//隐藏
    } else {
        return false
    }
}
//处理退款按钮的可见性
export function dealRefundBtns (trueBtn, falseBtn) {
    let isrefund = this.props.form.getFormItemsValue(this.formId, 'isrefund') ? this.props.form.getFormItemsValue(this.formId, 'isrefund').value : false
    if ((isrefund || isrefund == "Y") && isrefund != "N") {
        falseBtn.push('AddLine')
        falseBtn.push('Insert_inner')
    }
}

function removeArray (arr, key) {
    var index = arr.indexOf(key)
    if (index >= 0) {
        arr.splice(index, 1)
    }
}

/**
 * 设置生成有效开票申请单的应收单修改时，表体肩部和行按钮控制
 * @param {*} props 
 * @param {*} flag 
 */
function dealBtnVisibleForRecBill (props, flag) {
    // let status = props.getUrlParam('status');
    props.button.setButtonVisible([
        bodyButton.CopyLine, bodyButton.DelLine, bodyButton.AddLine, innerButton.Delete_inner,
        innerButton.Copy_inner, innerButton.Insert_inner
    ]
        , !flag)
    // 如果是编辑态时，展开按钮也需控制
    // if (status == 'edit') {
    //     props.button.setButtonVisible(innerButton.open_edit, !flag);
    // }
}

/**
 * 设置发票按钮显隐性控制
 * @param {*} props
 * @param {*} formId  
 * @param {*} flag 
 */
function dealBtnVisbleForInvocieTime (props, formId, flag) {
    let money = props.form.getFormItemsValue(formId, 'money').value
    // 金额为负的单据，红冲发票等特殊控制
    if (money < 0) {
        props.button.setButtonVisible([
            headButton.MaintainTaxInvoice, headButton.LinkTaxInvoice, headButton.RedInvoice
        ], !flag)
    } else {
        props.button.setButtonVisible([
            headButton.SpecialInvoice, headButton.GeneralInvoice, headButton.ElectronicInvoice,
            headButton.Electronspecinvoice, headButton.OilSpecialInvoice, headButton.OilGeneralInvoice, headButton.OilElectronicInvoice, headButton.VatinvoiceVehicleInvoice_99
        ], !flag)
    }
}


export {
    buttonVisible, getButtonsKey, getInnerButtonkey, cardGetbodyAndInnerbutton,
    cardBodyAndInnerButtonVisible, cardBodyControl, initCardBodyEditControl, onSelectedCardBodyEditControl, initCardBodybrowseControl,
    onListButtonControl, cardPullButtonControl, dealBtnVisibleForRecBill, dealBtnVisbleForInvocieTime
}

/*sywsGGtTLb/wDzM0jlIiWu/BZJAgfVEqY6VdTYu+giAwdBRWGXVHz9RXyell2zq+*/